package com.yunhan.utils;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.*;


/**
 * 线程池
 */
public class ExecutorsUtils {


	/**
	 * CPU核数
	 */
	private static final int cpu = Runtime.getRuntime().availableProcessors();

    /**
     * 缓存线程池
     */
    private static Executor cacheExecutor =  null;

    /**
     * 业务线程池
     */
    private static Executor bizExecutor =  null;

    static {
        ThreadPoolTaskExecutor cache = new ThreadPoolTaskExecutor();
        cache.setCorePoolSize(cpu);
        cache.setMaxPoolSize(cpu * 5);
        cache.initialize();
        cacheExecutor = cache;

        ThreadPoolTaskExecutor biz = new ThreadPoolTaskExecutor();
        biz.setCorePoolSize(cpu * 6);
        biz.setMaxPoolSize(cpu * 12);
        biz.initialize();
        bizExecutor = biz;
    }

    /**
     * 获取缓存线程池
     * @return
     */
    public static Executor getExecutorPool() {
        return cacheExecutor;
    }

    /**
     * 获取业务线程池
     * @return
     */
    public static Executor getBizExecutorPool() {
        return bizExecutor;
    }

}
